﻿layui.config({
    base: "Scripts/ApplyMange/"
}).use(['form', 'layer', 'jquery', 'laypage', 'table'], function () {
    var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage,
		$ = layui.jquery,
        table = layui.table,
		pageData = { PageSize: 10, PageCount: 0, PageNumber: 0, RowCount: 0, Where: "" };
    //每页显示数    总页数           当前页码          总行数        

    //表格数据和分页
    function getList(that) {
        $.ajax({
            type: "POST",
            url: "/ApplyMange/Index",
            data: that,
            dataType: "json",
            success: function (data) {
                var result = JSON.parse(data.Data);
                var usersData = result.DataList;
                pageData.PageCount = result.PageCount;
                pageData.PageNumber = result.PageNumber;
                pageData.PageSize = result.PageSize;
                pageData.RowCount = result.RowCount;
                //执行加载数据的方法
                renderDate(usersData);
                showPage(pageData);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.alert(textStatus);
            }
        });
        //渲染数据
        function renderDate(list) {
            var dataHtml = '';
            if (list != null) {
                layui.each(list, function (index, item) {
                    dataHtml += '<tr>'
                    + '<td>' + item.JobNumber + '</td>'
                    + '<td>' + item.Name + '</td>'
                    + '<td>' + (item.Sex == true ? "男" : "女") + '</td>'
                    + '<td>' + item.TypeName + '</td>'
                    + '<td>' + item.LeaveReason + '</td>'
                    + '<td>' + item.LeaveTime.substring(0, 10) + '</td>'
                    + '<td>' + item.EndTime.substring(0, 10) + '</td>'
                    + '<td>' + item.ApproveResult + '</td>'
                    + '<td>' + item.Remark + '</td>'
                    + '<td><a class="layui-btn layui-btn-primary layui-btn-xs news_details "  data-id="' + item.PLID + '"><i class="layui-icon">&#xe621;</i> 查看</a><a class="layui-btn layui-btn-xs news_edit" data-id="' + item.PLID + '"><i class="layui-icon">&#xe642;</i>编辑</a><a class="layui-btn layui-btn-danger layui-btn-xs news_del" data-id="' + item.PLID + '"><i class="layui-icon">&#xe640;</i> 删除</a></td>'
                    + '</tr>';
                });
            } else {
                dataHtml = '<tr><td colspan="11">暂无数据</td></tr>';
            }
            $(".news_content").html(dataHtml);   //渲染数据
            $('.news_list thead input[type="checkbox"]').prop("checked", false);
            form.render(); //渲染表单
        }

    }

    //分页
    function showPage(page) {
        laypage.render({
            elem: 'page',
            count: page.RowCount,        //得到总页数
            limit: page.PageSize,         //每页显示数量
            curr: page.PageNumber + 1,   //    页码
            groups: 5,                  //连续显示分页数
            jump: function (obj, first) {
                if (!first) {//防止无限刷新
                    pageData.PageNumber = obj.curr - 1;
                    getList(page);
                }
            }
        });
    };

    //查看按钮
    $("body").on("click", ".news_details", function () {
        var _this = $(this);
        layer.open({
            type: 2,        //基本层类型
            title: ['查看详细信息', 'background-color: #00bb9d;text-align:center;font-size:18px;color:#FFFFFF'],   //标题  标题+CSS样式
            shadeClose: true,             //遮罩关闭
            shade: [0.6, '#393D49'],       //遮罩
            maxmin: false,                //最大化最小化
            anim: 2,                     //动画
            area: ['800px', '500px'],    //宽高
            content: "/ApplyMange/Details?PLID=" + _this.attr("data-id"),   //内容      //控制器接收PLID
            success: function (layero, index) {
                var body = layer.getChildFrame('body', index);//建立父窗体和子窗体关系                
                body.find('input[id="PLID"]').val(_this.attr("data-id"));//访问子窗体元素，   //前台接收PLID
                //   注意会有延迟所以子窗体使用时要延迟加载
            },
            end: function () {
                //        如果是通过单击关闭按钮关闭弹出层，父画面没有此表单  
                if ($("#popupForm").length === 1) {
                    $("#popupForm").submit();
                }
            }
        });
    });

    //编辑按钮
    $("body").on("click", ".news_edit", function () {
        var _this = $(this);
        //location.href = '/ApplyMange/Edit?PLID=' + _this.attr("data-id");

        layer.open({
            type: 2,
            title: ['编辑信息', 'background-color: #00bb9d;text-align:center;font-size:18px;color:#FFFFFF'],
            shadeClose: true,
            shade: [0.6, '#393D49'],
            maxmin: false,
            anim: 4,
            data: { "PLID": _this.attr("data-id") },
            area: ['800px', '500px'],
            content: "/ApplyMange/Edit?PLID=" + _this.attr("data-id"),
            success: function (layero, index) {
                var body = layer.getChildFrame('body', index);//建立父窗体和子窗体关系                
                body.find('input[id="PLID"]').val(_this.attr("data-id"));//访问子窗体元素，
                //   注意会有延迟所以子窗体使用时要延迟加载
            },
            end: function () {
                //        如果是通过单击关闭按钮关闭弹出层，父画面没有此表单  
                if ($("#popupForm").length === 1) {
                    $("#popupForm").submit();
                }
                location.reload();   //页面刷新    --只要页面关闭 刷新父页面
            }
        });
    });

    //删除按钮
    $("body").on("click", ".news_del", function () {
        var _this = $(this);
        layer.confirm('确定删除此信息？', { icon: 3, title: '提示信息' }, function (index) {
            var index = layer.msg('删除中，请稍候', { icon: 16, time: false, shade: 0.8 });
            $.ajax({
                type: "post",
                url: "/ApplyMange/Delete",     //地址
                data: { "PLID": _this.attr("data-id") },
                success: function (result) {
                    var i = layer.msg(result.Message, { icon: 16, time: false, shade: 0.8 });
                    getList(pageData);
                    layer.close(i);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.alert(textStatus);
                }
            });
            layer.close(index);
        });
    });

    //请假类型下拉框
    function TypeSelect() {
        var url = "/ApplyMange/DropType";
        $.post(url, function (result) {

            var types = JSON.parse(result.data);
            var typesDrop = $("#leaveType");
            layui.each(types, function (index, data) {
                var typesOption = $("<option value='" + data.TypeID + "'>" + data.TypeName + "</option>");
                typesDrop.append(typesOption);
            });
            form.render();
        });
    };

    //搜索按钮
    form.on('submit(btnSelect)', function (data) {
        var Name = $("#username").val();
        var type = $("#leaveType option:selected").val();
        var state = $("#leaveState option:selected").text();
        if (Name != null) {
            pageData.Where = " Name like '%" + Name + "%'";
        }
        if (type != 0) {
            pageData.Where += "  and TypeID='" + type + "'";
        }
        if (state != "全部") {
            pageData.Where += " and  ApproveResult='" + state + "'";
        }
        getList(pageData);
        return false;
    });

    //关闭按钮
    $().ready(function () {
        $("#btnClose").click(function () {
            layer.closeAll();
        })
    });

    //事件执行
    $(function () {
        getList(pageData);
        TypeSelect();
    });
});




